/**
 * @file dev_lcd.c
 * @author lik
 * @brief lcd相关配置函数
 * @version 0.1
 * @date 2022-01-14
 *
 * @copyright Copyright (c) 2022
 *
 */

#include "main.h"
volatile uint8_t Flag_DMA_IRQ_Send = 0;
volatile uint8_t Flag_DMA_IRQ_Get = 0;
static inline void Delay(uint32_t ms)
{
	for(uint32_t i=0;i<ms;i++)	
		for (int us = 0; us < 1000; us++)
		__NOP();
}
#define UE024QV
//#define FW028TFT
void lcd_set_addr(uint16_t xs,uint16_t xe,uint16_t ys,uint16_t ye)
{
	  ys+=0x23;
	  ye+=0x23;	
    LCD_WR_REG(LCD, 0x2A);
    LCD_WR_DATA(LCD, xs>>8);
    LCD_WR_DATA(LCD, xs&0xFF);
    LCD_WR_DATA(LCD, xe>>8);
    LCD_WR_DATA(LCD, xe&0xFF);//X

    LCD_WR_REG(LCD, 0x2B);
    LCD_WR_DATA(LCD, ys>>8);
    LCD_WR_DATA(LCD, ys&0xFF);
    LCD_WR_DATA(LCD, ye>>8);
    LCD_WR_DATA(LCD, ye&0xFF);//Y
   
    LCD_WR_REG(LCD, 0x2C); //display on 
}

void lcd_draw_point(uint16_t x, uint16_t y, uint16_t c )
{
    LCD_WR_REG(LCD, 0x2A); //Set_column_address
    LCD_WR_DATA(LCD, x>>8);
    LCD_WR_DATA(LCD, x&0xFF);
    LCD_WR_DATA(LCD, x>>8);
    LCD_WR_DATA(LCD, x&0xFF);

    LCD_WR_REG(LCD, 0x2B); //Set_page_address
    LCD_WR_DATA(LCD, y>>8);
    LCD_WR_DATA(LCD, y&0xFF);
    LCD_WR_DATA(LCD, y>>8);
    LCD_WR_DATA(LCD, y&0xFF);

    LCD_WR_REG(LCD, 0x2C); //display on 
    LCD_WR_DATA(LCD, c);
}

/**
 * @brief lcd初始化
 *
 */
void lcd_init(void)
{
    MPULCD_InitStructure MPULCD_initStruct;
    /** 复位LCD模块 */
    GPIO_Init(GPIOD, PIN2, 1, 0, 0, 0);
    GPIO_ClrBit(GPIOD, PIN2);
    swm_delay_ms(1);
    GPIO_SetBit(GPIOD, PIN2);
    /** 点亮背光 */
    GPIO_Init(GPIOM, PIN0, 1, 0, 0, 0);
    GPIO_SetBit(GPIOM, PIN0);

    PORT_Init(PORTA, PIN1, PORTA_PIN1_LCD_B3, 0); // MPU_D0
    PORT_Init(PORTA, PIN2, PORTA_PIN2_LCD_B4, 0);
    PORT_Init(PORTA, PIN9, PORTA_PIN9_LCD_B5, 0);
    PORT_Init(PORTA, PIN10, PORTA_PIN10_LCD_B6, 0);
    PORT_Init(PORTA, PIN0, PORTA_PIN0_LCD_B7, 0);

    PORT_Init(PORTA, PIN14, PORTA_PIN14_LCD_G2, 0);
    PORT_Init(PORTA, PIN15, PORTA_PIN15_LCD_G3, 0);
    PORT_Init(PORTC, PIN0, PORTC_PIN0_LCD_G4, 0);
    PORT_Init(PORTC, PIN1, PORTC_PIN1_LCD_G5, 0);
    PORT_Init(PORTC, PIN2, PORTC_PIN2_LCD_G6, 0);
    PORT_Init(PORTC, PIN3, PORTC_PIN3_LCD_G7, 0);

    PORT_Init(PORTN, PIN5, PORTN_PIN5_LCD_R3, 0);
    PORT_Init(PORTD, PIN0, PORTD_PIN0_LCD_R4, 0);
    PORT_Init(PORTD, PIN1, PORTD_PIN1_LCD_R5, 0);
    PORT_Init(PORTC, PIN12, PORTC_PIN12_LCD_R6, 0);
    PORT_Init(PORTC, PIN13, PORTC_PIN13_LCD_R7, 0); // MPU_D15

    PORT_Init(PORTM, PIN4, PORTM_PIN4_LCD_CS, 0);
    PORT_Init(PORTM, PIN8, PORTM_PIN8_LCD_WR, 0);
    PORT_Init(PORTM, PIN11, PORTM_PIN11_LCD_RS, 0);
    PORT_Init(PORTN, PIN0, PORTN_PIN0_LCD_RD, 0);

    MPULCD_initStruct.RDHoldTime = 10;
    MPULCD_initStruct.WRHoldTime = 3;
    MPULCD_initStruct.CSFall_WRFall = 3;
    MPULCD_initStruct.WRRise_CSRise = 3;
    MPULCD_initStruct.RDCSRise_Fall = 2;
    MPULCD_initStruct.WRCSRise_Fall = 2;
    MPULCD_Init(LCD, &MPULCD_initStruct);

//    LCD_WR_REG(LCD, 0xb2);
//    LCD_WR_DATA(LCD, 0x0c);
//    LCD_WR_DATA(LCD, 0x0c);
//    LCD_WR_DATA(LCD, 0x00);
//    LCD_WR_DATA(LCD, 0x33);
//    LCD_WR_DATA(LCD, 0x33);
//    LCD_WR_REG(LCD, 0xb7);
//    LCD_WR_DATA(LCD, 0x35);

//    LCD_WR_REG(LCD, 0xbb);
//    LCD_WR_DATA(LCD, 0x2b);
//    LCD_WR_REG(LCD, 0xc3);
//    LCD_WR_DATA(LCD, 0x17);
//    LCD_WR_REG(LCD, 0xc4);
//    LCD_WR_DATA(LCD, 0x20);
//    LCD_WR_REG(LCD, 0xc6); // NLA2 NLA1 NLA0 (0x00: dot/0x07: column) RTNA4  RTNA3 RTNA2 RTNA1 RTNA0    Frame
//    LCD_WR_DATA(LCD, 0x0f);

//    LCD_WR_REG(LCD, 0x3A);
//    LCD_WR_DATA(LCD, 0x55);
//    LCD_WR_REG(LCD, 0x36);
//#ifdef UE024QV
//    LCD_WR_DATA(LCD, 0x48);
//#endif
//#ifdef FW028TFT
//    LCD_WR_DATA(LCD, 0x00);
//#endif

//    LCD_WR_REG(LCD, 0xd0);
//    LCD_WR_DATA(LCD, 0xa4);
//    LCD_WR_DATA(LCD, 0xa2);

//    LCD_WR_REG(LCD, 0xe0);
//    LCD_WR_DATA(LCD, 0xf0);
//    LCD_WR_DATA(LCD, 0x00);
//    LCD_WR_DATA(LCD, 0x0a);
//    LCD_WR_DATA(LCD, 0x10);
//    LCD_WR_DATA(LCD, 0x12);
//    LCD_WR_DATA(LCD, 0x1b);
//    LCD_WR_DATA(LCD, 0x39);
//    LCD_WR_DATA(LCD, 0x44);
//    LCD_WR_DATA(LCD, 0x47);
//    LCD_WR_DATA(LCD, 0x28);
//    LCD_WR_DATA(LCD, 0x12);
//    LCD_WR_DATA(LCD, 0x10);
//    LCD_WR_DATA(LCD, 0x16);
//    LCD_WR_DATA(LCD, 0x1b);
//    LCD_WR_REG(LCD, 0xe1);
//    LCD_WR_DATA(LCD, 0xf0);
//    LCD_WR_DATA(LCD, 0x00);
//    LCD_WR_DATA(LCD, 0x0a);
//    LCD_WR_DATA(LCD, 0x10);
//    LCD_WR_DATA(LCD, 0x11);
//    LCD_WR_DATA(LCD, 0x1a);
//    LCD_WR_DATA(LCD, 0x3b);
//    LCD_WR_DATA(LCD, 0x34);
//    LCD_WR_DATA(LCD, 0x4e);
//    LCD_WR_DATA(LCD, 0x3a);
//    LCD_WR_DATA(LCD, 0x17);
//    LCD_WR_DATA(LCD, 0x16);
//    LCD_WR_DATA(LCD, 0x21);
//    LCD_WR_DATA(LCD, 0x22);
//    
//    LCD_WR_REG(LCD, 0x11);
//    LCD_WR_REG(LCD, 0x29);
			Delay(120);                //ms

			LCD_WR_REG(LCD, 0x11);     

				 

			Delay(120);                //ms

			LCD_WR_REG(LCD, 0x36);     
			LCD_WR_DATA(LCD, 0x20);   

			LCD_WR_REG(LCD, 0x3A);     
			LCD_WR_DATA(LCD, 0x05);   

			LCD_WR_REG(LCD, 0xB2);     
			LCD_WR_DATA(LCD, 0x0C);   
			LCD_WR_DATA(LCD, 0x0C);   
			LCD_WR_DATA(LCD, 0x00);   
			LCD_WR_DATA(LCD, 0x33);   
			LCD_WR_DATA(LCD, 0x33);   

			LCD_WR_REG(LCD, 0xB7);     
			LCD_WR_DATA(LCD, 0x35);   

			LCD_WR_REG(LCD, 0xBB);     //VCOM  
			LCD_WR_DATA(LCD, 0x1A);   

			LCD_WR_REG(LCD, 0xC0);     
			LCD_WR_DATA(LCD, 0x2C);   

			LCD_WR_REG(LCD, 0xC2);     
			LCD_WR_DATA(LCD, 0x01);   

			LCD_WR_REG(LCD, 0xC3);     //GVDD  
			LCD_WR_DATA(LCD, 0x0B);   

			LCD_WR_REG(LCD, 0xC4);     
			LCD_WR_DATA(LCD, 0x20);   

			LCD_WR_REG(LCD, 0xC6);     
			LCD_WR_DATA(LCD, 0x0F);   

			LCD_WR_REG(LCD, 0xD0);     
			LCD_WR_DATA(LCD, 0xA4);   
			LCD_WR_DATA(LCD, 0xA1); 

			LCD_WR_REG(LCD, 0xD6);     
			LCD_WR_DATA(LCD, 0xA1); 

			LCD_WR_REG(LCD, 0xE0);     
			LCD_WR_DATA(LCD, 0xF0);   
			LCD_WR_DATA(LCD, 0x06);   
			LCD_WR_DATA(LCD, 0x0B);   
			LCD_WR_DATA(LCD, 0x07);   
			LCD_WR_DATA(LCD, 0x07);   
			LCD_WR_DATA(LCD, 0x24);   
			LCD_WR_DATA(LCD, 0x2E);   
			LCD_WR_DATA(LCD, 0x32);   
			LCD_WR_DATA(LCD, 0x46);   
			LCD_WR_DATA(LCD, 0x37);   
			LCD_WR_DATA(LCD, 0x13);   
			LCD_WR_DATA(LCD, 0x13);   
			LCD_WR_DATA(LCD, 0x2D);   
			LCD_WR_DATA(LCD, 0x33);   

			LCD_WR_REG(LCD, 0xE1);     
			LCD_WR_DATA(LCD, 0xF0);   
			LCD_WR_DATA(LCD, 0x02);   
			LCD_WR_DATA(LCD, 0x06);   
			LCD_WR_DATA(LCD, 0x09);   
			LCD_WR_DATA(LCD, 0x08);   
			LCD_WR_DATA(LCD, 0x05);   
			LCD_WR_DATA(LCD, 0x29);   
			LCD_WR_DATA(LCD, 0x44);   
			LCD_WR_DATA(LCD, 0x42);   
			LCD_WR_DATA(LCD, 0x38);   
			LCD_WR_DATA(LCD, 0x14);   
			LCD_WR_DATA(LCD, 0x14);   
			LCD_WR_DATA(LCD, 0x2A);   
			LCD_WR_DATA(LCD, 0x30);   

			LCD_WR_REG(LCD, 0x21);     

			LCD_WR_REG(LCD, 0x29);    
    
}

void LCD_Display_DMA_SPIFlash(int xs,int ys, uint16_t ID)
{
    int img_w,img_h;
    uint32_t Addr = ImgMap[ID][0];
    
    img_w = ImgMap[ID][1];
    img_h = ImgMap[ID][2];
    
    lcd_set_addr(xs,(xs+img_w)-1,ys,(ys+img_h)-1);
    MPULCD_DMAStart(LCD, (uint32_t *)(SFLASH_BASE+Addr), img_w, img_h);
    while(MPULCD_DMABusy(LCD));
		swm_delay_ms(2);
}


static uint32_t Send_Done(void)
{
	if(DMA_CH_INTStat(DMA_CH1, DMA_IT_DONE))
	{
		DMA_CH_INTClr(DMA_CH1, DMA_IT_DONE);
		
		return 1;
	}
	else
	{
		return 0;
	}
}

void ST7789_DMA_PIC(int xs,int ys, uint16_t ID)
{	
	int img_w,img_h;
	uint32_t Addr = ImgMap[ID][0];
	
	img_w = ImgMap[ID][1];
	img_h = ImgMap[ID][2];
	
	DMA_InitStructure DMA_initStruct;
	
	
	
	st7789_set_addr(xs,ys,(xs+img_w)-1,(ys+img_h)-1);

	ST7789_CS_OFF;
	ST7789_DC_ON; //写数据
	
	SPI1->CTRL &= ~SPI_CTRL_DMATXEN_Msk;
	SPI1->CTRL |= (1 << SPI_CTRL_DMATXEN_Pos);
	DMA_initStruct.Mode = DMA_MODE_SINGLE;
	DMA_initStruct.Unit = DMA_UNIT_HALFWORD;
	DMA_initStruct.Count = (img_w * img_h);
	DMA_initStruct.SrcAddr = (uint32_t)(SFLASH_BASE+Addr);
	DMA_initStruct.SrcAddrInc = 1;
	DMA_initStruct.DstAddr = (uint32_t)&SPI1->DATA;
	DMA_initStruct.DstAddrInc = 0;
	DMA_initStruct.Handshake = DMA_CH1_SPI1TX;
	DMA_initStruct.Priority = DMA_PRI_HIGH;
	DMA_initStruct.INTEn = 1;
	DMA_CH_Init(DMA_CH1, &DMA_initStruct);
	DMA_CH_Open(DMA_CH1);
	DMA->CH[DMA_CH1].CR &= ~(DMA_CR_TXEN_Msk | DMA_CR_RXEN_Msk|DMA_CR_LEN_Msk);		//关闭DMA
	DMA->CH[DMA_CH1].CR |= (((img_w * img_h) ) << DMA_CR_LEN_Pos);
	DMA->CH[DMA_CH1].SRC = (uint32_t)(SFLASH_BASE+Addr);														//设置起始地址
	DMA->CH[DMA_CH1].CR |= (1 << DMA_CR_RXEN_Pos);									//打开DMA
	Flag_DMA_IRQ_Send = 0;
	
	//while(Send_Done() == 0);
	while(Flag_DMA_IRQ_Send == 0);
	ST7789_CS_ON;
}




void SPI_DMA_SendGet_Init(void)
{
	DMA_InitStructure DMA_initStruct;
	
	
	 static uint16_t rx_data;
	//	DMA_InitStructure DMA_initStruct;
	//SPI0 RX DMA
	DMA_initStruct.Mode = DMA_MODE_SINGLE;
	DMA_initStruct.Unit = DMA_UNIT_WORD;
	DMA_initStruct.Count = Len_Buf/4+1;
//	DMA_initStruct.SrcAddr = (uint32_t)&SPI1->DATA;
	DMA_initStruct.SrcAddrInc = 1;
//	DMA_initStruct.DstAddr = (uint32_t)&rx_data;
	DMA_initStruct.DstAddrInc = 1;
	DMA_initStruct.Handshake = 0;
	DMA_initStruct.Priority = DMA_PRI_HIGH;
	DMA_initStruct.INTEn = DMA_IT_DONE;
	DMA_CH_Init(DMA_CH0, &DMA_initStruct);
	
//	DMA_CH_Open(DMA_CH0);
	
//	/*发送到LCD*/		
//	SPI1->CTRL &= ~SPI_CTRL_DMATXEN_Msk;
//	SPI1->CTRL |= (1 << SPI_CTRL_DMATXEN_Pos);
//	DMA_initStruct.Mode = DMA_MODE_SINGLE;
//	DMA_initStruct.Unit = DMA_UNIT_HALFWORD;
//	DMA_initStruct.Count = Len_Buf/2;
////	DMA_initStruct.SrcAddr = 0;
//	DMA_initStruct.SrcAddrInc = 1;
//	DMA_initStruct.DstAddr = (uint32_t)&SPI1->DATA;
//	DMA_initStruct.DstAddrInc = 0;
//	DMA_initStruct.Handshake = DMA_CH1_SPI1TX;
//	DMA_initStruct.Priority = DMA_PRI_LOW;
//	DMA_initStruct.INTEn = DMA_IT_DONE;
//	DMA_CH_Init(DMA_CH1, &DMA_initStruct);
////	DMA_CH_Open(DMA_CH1);
	
	



	
}

void SPI_FLASH_Get(uint32_t ReadAddr,uint32_t *temp_buf)
{	

	
	
	//DMA配置
	DMA->CH[DMA_CH0].CR &= ~(DMA_CR_TXEN_Msk | DMA_CR_RXEN_Msk);		//关闭DMA
  DMA->CH[DMA_CH0].SRC = (uint32_t)ReadAddr;														//设置起始地址
	DMA->CH[DMA_CH0].DST = (uint32_t)temp_buf;														//设置起始地址
	DMA->CH[DMA_CH0].CR |= (1 << DMA_CR_RXEN_Pos);									//打开DMA
	
	//辅助变量
	Flag_DMA_IRQ_Get = 0;
}


void LCD_DMA_Send(uint16_t *RPG)
{		
	DMA_InitStructure DMA_initStruct;	
	//重启SPI的DMA使能
	SPI1->CTRL &= ~SPI_CTRL_DMATXEN_Msk;
	SPI1->CTRL |= (1 << SPI_CTRL_DMATXEN_Pos);
	
	DMA->CH[DMA_CH1].CR &= ~(DMA_CR_TXEN_Msk | DMA_CR_RXEN_Msk |DMA_CR_LEN_Msk);		//关闭DMA

	DMA->CH[DMA_CH1].SRC = (uint32_t)RPG;														//设置起始地址
	DMA->CH[DMA_CH1].CR |= (1 << DMA_CR_RXEN_Pos);									//打开DMA
	

	
	//辅助标志	
	Flag_DMA_IRQ_Send = 0;
	

	
}


		uint8_t LCD_buf_1[Len_Buf];
		uint8_t LCD_buf_2[Len_Buf];
void LCD_Display_SPIDMA_SPIFlash(int xs,int ys, uint16_t ID)
{
		uint8_t LCD_buf_1[Len_Buf];
		uint8_t LCD_buf_2[Len_Buf];
	
		int img_w,img_h;
		uint32_t i=0;
		uint32_t Addr = ImgMap[ID][0];
		uint8_t FlagSend = 0;
		
		img_w = ImgMap[ID][1];
		img_h = ImgMap[ID][2];
	
		uint32_t times = (img_w * img_h) *2 / Len_Buf;	/* 循环次数 */
		uint32_t remain = (img_w * img_h) *2 - times * Len_Buf;

		st7789_set_addr(xs,ys,(xs+img_w)-1,(ys+img_h)-1);

		ST7789_CS_OFF;
		ST7789_DC_ON; //写数据
		
		//先获取一包数据
		SPI_FLASH_Get((SFLASH_BASE+Addr),(uint32_t *)LCD_buf_1);
		while(Flag_DMA_IRQ_Get == 0 );
		
		uint32_t PackId = 0;
		for(i = 0;i < times;i++)
		{
			PackId++;

			if(FlagSend == 0)
			{
				FlagSend = 1;
				SPI_TFT_Display((uint16_t*) LCD_buf_1, Len_Buf/2);
				SPI_FLASH_Get((SFLASH_BASE+Addr +(PackId * Len_Buf)),(uint32_t *)LCD_buf_2);
			}
			else
			{
				FlagSend = 0;
				SPI_TFT_Display((uint16_t*) LCD_buf_2, Len_Buf/2);				
				SPI_FLASH_Get((SFLASH_BASE+Addr +(PackId * Len_Buf)),(uint32_t *)LCD_buf_1);				
			}

			while(Flag_DMA_IRQ_Get == 0 || Flag_DMA_IRQ_Send == 0);		
			
		}
		/* 20231225---余数判断 */
		if(remain)
		{
			SPI_FLASH_Get((SFLASH_BASE+Addr +(PackId * Len_Buf)),(uint32_t *)LCD_buf_2);
			while(Flag_DMA_IRQ_Get == 0)__NOP();
			Flag_DMA_IRQ_Get = 0;
			SPI_TFT_Display((uint16_t *)LCD_buf_2, remain/2);	
			while(Flag_DMA_IRQ_Send == 0)__NOP();
			Flag_DMA_IRQ_Send = 0;
		}
		ST7789_CS_ON;
}


void DMA_Handler(void)
{	
		//SPI rx lcd
	if(DMA_CH_INTStat(DMA_CH0,DMA_IT_DONE))
	{
		DMA_CH_INTClr(DMA_CH0,DMA_IT_DONE);
		Flag_DMA_IRQ_Get = 1;
	}
	//SPI tx lcd
	if(DMA_CH_INTStat(DMA_CH1,DMA_IT_DONE))
	{
		DMA_CH_INTClr(DMA_CH1,DMA_IT_DONE);
		Flag_DMA_IRQ_Send = 1;
	}
	
}
void ShowPic(uint16_t x,uint16_t y,uint8_t num)
{
	LCD_Display_SPIDMA_SPIFlash(x,y,EX_BAT_003+num);
}

void ShowStateALL(uint8_t mode,uint8_t speed,uint8_t temp,uint8_t lock)
{
	ShowPic(ModeSX,ModeSY,ModeNum+mode);
	
	if(speed>0)
		ShowPic(SpeedSX,SpeedSY,SpeedNum+speed);//风速档位
	
	if(temp>0)
		ShowPic(TemperatureSX,TemperatureSY,TemperatureNum+temp);//温度
	
	if(lock==1)
		ShowPic(LockSX,LockSY,LockNum);  //锁标
}

uint32_t tickstart=0;
uint32_t tickend=0;
uint32_t LCD_buf_temp[Len_Buf/4];
uint32_t showtime=1;
void LCD_Test(void)
{
	while(showtime)
	{
		tickstart = swm_gettick();
		for(uint32_t i=1;i<LogoNum;i++)
		{
			LCD_Display_SPIDMA_SPIFlash(0,0,EX_BAT_003+i);
			swm_delay_ms(100);
		}	
		tickend=swm_gettick()-tickstart;

		if(showtime>0)
			showtime--;
	}

	ShowStateALL(1,0,0,0);
}

uint8_t DonghuaNum;
void Donghua(uint8_t startPicNum,uint8_t allPicNum)
{
	
	ShowPic(DonghuaSX,DonghuaSY,startPicNum+DonghuaNum);

	DonghuaNum++;
	if(DonghuaNum>=allPicNum)
		DonghuaNum = 0;
}

void Color_Test(void)
{
	while(1)
    {
        lcd_set_addr(0,359,0,359);
        for (uint32_t i=0;i<360;i++)
        {
            for (uint32_t j=0;j<360;j++)
            {
                LCD_WR_DATA(LCD, 0x001F);
            }
        }
        swm_delay_ms(1000);
        lcd_set_addr(0,359,0,359);
        for (uint32_t i=0;i<360;i++)
        {
            for (uint32_t j=0;j<360;j++)
            {
                LCD_WR_DATA(LCD, 0x07E0);
            }
        }
        swm_delay_ms(1000);
        lcd_set_addr(0,359,0,359);
        for (uint32_t i=0;i<360;i++)
        {
            for (uint32_t j=0;j<360;j++)
            {
                LCD_WR_DATA(LCD, 0xF800);
            }
        }
        swm_delay_ms(1000);
    }
}	